
Partial Class Welcome
    Inherits System.Web.UI.Page

    Protected Sub SignupBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SignUpBtn.Click

        Dim userName As String = UNTxtB.Text.Trim
        Dim userPassword As String = UPTxtB.Text.Trim
        Dim userEmail As String = UETxtB.Text.Trim

        ErrorList.Rows.Clear()

        If (Not AreDetailsValid(userName, userPassword, userEmail)) Then

        Else
            'insert the username and password and email add and get userId
            Dim user As User = DBConnection.FET_USER(userName, userPassword)

            If (user Is Nothing) Then

                Dim id As Integer = DBConnection.INS_NEW_USER(userName, userPassword, userEmail, "<Setup />")

                If id = -1 Then
                    PopulateErrorList("User Name and Password", "", "Invalid. An error occurred. Try again.")

                Else '
                    Session.Add(CStr(id), id)
                    Response.Redirect("SimulationPage.aspx?id=" & id)
                    'Server.Transfer("SimulationPage.aspx?id=" & id)
                End If


            Else

                PopulateErrorList("User Name and Password", "", "Invalid. An error occurred. Try again.")


            End If

        End If
    End Sub

    Protected Sub LoginBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginBtn.Click


        Dim userName As String = LUNTxtB.Text.Trim
        Dim userPassword As String = LUPTxtB.Text.Trim

        ErrorList.Rows.Clear()

        If (Not AreDetailsValid(userName, userPassword, Nothing)) Then

        Else

            Dim user As User = DBConnection.FET_USER(userName, userPassword)
            If (user Is Nothing) Then
                PopulateErrorList("User Name and Password", "", "Invalid. Sign up?")

            Else
                Session.Add(CStr(user.ID), user.ID)
                Response.Redirect("SimulationPage.aspx?id=" & user.ID)

            End If

        End If


    End Sub

    Private Function AreDetailsValid(ByVal userName As String, ByVal userPassword As String, ByVal userEmail As String) As Boolean

        Dim ok As Boolean = True
        If Not userName Is Nothing Then
            If (HasLegalChars(userName)) Then


                If (HasCorrectSize(userName)) Then

                Else

                    PopulateErrorList("User Name", userName, "Cannot have more than 20 characters")
                    ok = False
                End If
            Else
                PopulateErrorList("User Name", userName, "Must contain ALPHA-NUMERIC characters only")
                ok = False
            End If




        End If

        If Not userPassword Is Nothing Then
            If (HasLegalChars(userPassword)) Then


                If (HasCorrectSize(userPassword)) Then

                Else
                    PopulateErrorList("User Password", userPassword, "Cannot have more than 20 Chars")
                    ok = False
                End If
            Else
                PopulateErrorList("User Password", userPassword, "Must contain ALPHA-NUMERIC characters only")
                ok = False
            End If
        End If

        If Not userEmail Is Nothing Then
            If Not HasValidEmail(userEmail) Then
                PopulateErrorList("User Email", userEmail, "Invalid email address used")
                ok = False
            End If
        End If


        Return ok
    End Function

    Private Function PopulateErrorList(ByVal fieldTxt As String, ByVal valueTxt As String, ByVal msgTxt As String) As Boolean


        Dim field As New Label
        Dim value As New Label
        Dim message As New Label


        field.Text = fieldTxt
        value.Text = valueTxt
        message.Text = msgTxt

        Dim cell1 As New TableCell
        Dim cell2 As New TableCell
        Dim cell3 As New TableCell

        cell1.Controls.Add(field)
        cell2.Controls.Add(value)
        cell3.Controls.Add(message)

        Dim tblRow As New TableRow
        tblRow.Cells.Add(cell1)
        tblRow.Cells.Add(cell2)
        tblRow.Cells.Add(cell3)


        ErrorList.Rows.Add(tblRow)
    End Function



    'username and userpassword must have only letters and/or numbers
    Private Function HasLegalChars(ByVal data As String) As Boolean

        Dim chrArray() As Char
        chrArray = Data.ToCharArray

        'Iterate through the string.
        For counter As Integer = 0 To chrArray.Length - 1

            If Not Char.IsLetter(chrArray(counter)) And Not Char.IsNumber(chrArray(counter)) Then
                Return False
            End If
        Next
        Return True
    End Function

    'username and userpassword CANNOT be more than 20 chars long
    Private Function HasCorrectSize(ByVal data As String) As Boolean

        If data.Length > 20 Then
            Return False
        End If

        Return True
    End Function

    Private Function HasValidEmail(ByVal email As String) As Boolean
        Dim pattern As String
        pattern = "^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"

        If Regex.IsMatch("feedback@net-informations.com", pattern) Then
            'MsgBox("Valid Email address ")
            Return True
        Else
            'MsgBox("Not a valid Email address ")
            Return False
        End If

    End Function


    Public Property GetHidden() As Integer
        Get
            Return hidden.Value
        End Get
        Set(ByVal value As Integer)
            '
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim version As String = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()

        versionLbl.Text = "1" '"Version #: " & version
    End Sub
End Class
